import React, { createContext,useContext, useEffect } from 'react';
import ReactDOM from 'react-dom/client';
import {message} from 'antd';
import './index.css';

import { RouterProvider } from 'react-router-dom';
import router from './router/router-index';
import MicorAppContext from './context/micro-app-context';
import { observer } from 'mobx-react';
import microData from './store/micro-data';

type WindowWithCustomVars = Window & {
    __POWERED_BY_QIANKUN__: {
        onGlobalStateChange:(params:((state:any)=>void))=>void,
        setGlobalState:(params:any)=>void
    };
};
  
// 声明 window 为新的类型
declare let window: WindowWithCustomVars;


const App = ()=>{
    const micorApp:any = useContext(MicorAppContext);


 
    useEffect(()=>{
        if(microData.microServiceData.from === 'mainApp'){
            console.log(`microData.microServiceData = ${JSON.stringify(microData.microServiceData)}`);
            message.info(`子应用收到来自主应用的消息 message = ${microData.microServiceData.message}--microData.microServiceData.state = ${JSON.stringify(microData.microServiceData.state)}`);
        }
    },[microData.microServiceData]);
    

    /*
    useEffect(()=>{
        if (window.__POWERED_BY_QIANKUN__) {
            micorApp.onGlobalStateChange((data:any,prev:any)=>{
                if(data.from === 'mainApp'){
                    microData.updateMicroServiceData(data);
                }
            });
        }  
    },[]);
    */
    
    return <React.StrictMode>
        <RouterProvider router={router} />
    </React.StrictMode>
}


export default observer(App);